package com.itbour.web.action.stats;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dao.stats.KeywordTmplCnt;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.stats.SysTotalStatisDto;
import com.itbour.dto.stats.SysUserInfo;
import com.itbour.dto.stats.UserDocInfoDto;
import com.itbour.service.stats.SysInfoStatsService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 获取系统分析数据
 * @author LQ
 */
public class GetSysInfoStatsAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private SysInfoStatsService sysInfoStatsService;
	private List<SysUserInfo> listU;
	private List<SysTotalStatisDto> listTotal;
	private String findSdat;//用户起始时间
	private String findEdat;//用户截止时间
	private String finGrpBy;//1代表每日 2代表每周 3代表每月
	private String findPage;//条数
	private List<UserDocInfoDto> listUd;
	private String findDocDts;
	private String findDocDte;
	private static String stS = " 00:00:00";
	private static String stE = " 23:59:59";
	
	
	
	//获取用户数量增长统计图
	public String getSysUserChartInfo(){
		Map<String, Object> parm =new HashMap<String, Object>();
		if(StringUtils.isNotEmpty(finGrpBy)){
			if(1==Integer.parseInt(finGrpBy)){
				parm.put("grpby","day");
			}
			if(2==Integer.parseInt(finGrpBy)){
				parm.put("grpby","week");
			}
			if(3==Integer.parseInt(finGrpBy)){
				parm.put("grpby","month");
			}
		}else{
			parm.put("grpby","day");
		}
		if(StringUtils.isNotEmpty(findSdat)){
			parm.put("start_time",findSdat+stS);
		}
		if(StringUtils.isNotEmpty(findEdat)){
			parm.put("end_time",findEdat+stE);
		}else{
			Date dt =new Date();
			SimpleDateFormat sim =new SimpleDateFormat("yyyy-MM-dd");
			String end_time = sim.format(dt);
			parm.put("end_time", end_time+stE);
		}
		if(StringUtils.isNotEmpty(findPage)){
			parm.put("page",Integer.parseInt(findPage));
		}
		
		List<SysUserInfo> list =new ArrayList<SysUserInfo>();
		List<SysUserInfo> temp =new ArrayList<SysUserInfo>();
		list=sysInfoStatsService.getUserChartInfo(parm);
		Collections.sort(list);
		Integer len =list.size();//计算数组长度
		int page =Integer.parseInt(findPage);
		if(len>page){//取数据
			int i = len/page;//100/10
			for(int j=1;j<page;j++){
				temp.add(list.get(j*i));//获取
			}
			temp.add(list.get(0));
			temp.add(list.get(len-1));
			listU=temp;
		}else{
			listU=list;
		}
//		listU=sysInfoStatsService.getUserChartInfo(parm);
		return SUCCESS;
	}
	
	//用户文档统计
	public String getNumUserDocInfo() throws Exception{
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageSize = request.getParameter("rows");
			String grpBy=request.getParameter("grpBy");// day,week,month
			if(StringUtils.isNotBlank(grpBy)) {
				Map<String, Object> param = new HashMap<String, Object>();
				if(1==Integer.parseInt(grpBy)){
					param.put("grpBy","day");
				}
				if(2==Integer.parseInt(grpBy)){
					param.put("grpBy","week");
				}
				if(3==Integer.parseInt(grpBy)){
					param.put("grpBy","month");
				}
				if (StringUtils.isNotBlank(findDocDts)) {
					param.put("start_time", findDocDts+stS);
				}
				if (StringUtils.isNotBlank(findDocDte)) {
					param.put("end_time", findDocDte+stE);
				}else{
					Date dt =new Date();
					SimpleDateFormat sim =new SimpleDateFormat("yyyy-MM-dd");
					String end_time = sim.format(dt);
					param.put("end_time", end_time+stE);
				}
				List<UserDocInfoDto> list =new ArrayList<UserDocInfoDto>();
				List<UserDocInfoDto> temp =new ArrayList<UserDocInfoDto>();
				list=sysInfoStatsService.getUserDocCnt(param);
				Collections.sort(list);
				Integer len =list.size();//计算数组长度
				int page =Integer.parseInt(pageSize);
				if(len>page){
					int i = len/page;//100/10
					for(int j=1;j<page;j++){
						temp.add(list.get(j*i));//获取
					}
					temp.add(list.get(0));
					temp.add(list.get(len-1));
					listUd=temp;
				}else{
					listUd=list;
				}
			} else {
				listUd.clear();
			}
		} else {
			listUd.clear();
		}
		return SUCCESS;
	}
	
	/**
	 * 李琦
	 * 获取关键词下的模板个数
	 * @return
	 */
	private String findTopType;// 顶级分类
	private String findType;// 小分类
	private List<KeywordTmplCnt> listkeyTmplCnt;//
	public String getKeywordTmplCnt(){
		Map<String, Object> session = ActionContext.getContext().getSession();// 获取session
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> parm = new HashMap<String, Object>();
			if (StringUtils.isNotEmpty(findTopType)){
				parm.put("key_type_id", findTopType);
			}
			if (StringUtils.isNotEmpty(findType)){
				parm.put("type_id", findType);
			}
			listkeyTmplCnt = sysInfoStatsService.getKeywordTmplCnt(parm);
		} else {
			System.out.println("用户信息为空");
		}
		return SUCCESS;
	}
	
	/**	一键秀 模板关键词统计
	 */
	public String getYjxKeywordTmplCnt(){
		Map<String, Object> session = ActionContext.getContext().getSession();// 获取session
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user!=null && user.getUser_id()!=null){
			Map<String, Object> parm = new HashMap<String, Object>();
			listkeyTmplCnt = sysInfoStatsService.getYjxKeywordTmplCnt(parm);
		} 
		return SUCCESS;
	}
	
	//获取总的数量 显示
	public String getTotalStatisInfo(){
		Map<String, Object> parm =new HashMap<String, Object>();
		listTotal=sysInfoStatsService.getTotalStaList(parm);
		return SUCCESS;
	}


	public SysInfoStatsService getSysInfoStatsService() {
		return sysInfoStatsService;
	}
	public void setSysInfoStatsService(SysInfoStatsService sysInfoStatsService) {
		this.sysInfoStatsService = sysInfoStatsService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public List<SysUserInfo> getListU() {
		return listU;
	}
	public void setListU(List<SysUserInfo> listU) {
		this.listU = listU;
	}
	public List<SysTotalStatisDto> getListTotal() {
		return listTotal;
	}
	public void setListTotal(List<SysTotalStatisDto> listTotal) {
		this.listTotal = listTotal;
	}
	public String getFindSdat() {
		return findSdat;
	}
	public void setFindSdat(String findSdat) {
		this.findSdat = findSdat;
	}
	public String getFindEdat() {
		return findEdat;
	}
	public void setFindEdat(String findEdat) {
		this.findEdat = findEdat;
	}
	public String getFinGrpBy() {
		return finGrpBy;
	}
	public void setFinGrpBy(String finGrpBy) {
		this.finGrpBy = finGrpBy;
	}
	public String getFindPage() {
		return findPage;
	}
	public void setFindPage(String findPage) {
		this.findPage = findPage;
	}

	public List<UserDocInfoDto> getListUd() {
		return listUd;
	}
	public void setListUd(List<UserDocInfoDto> listUd) {
		this.listUd = listUd;
	}

	public String getFindDocDts() {
		return findDocDts;
	}
	public void setFindDocDts(String findDocDts) {
		this.findDocDts = findDocDts;
	}
	public String getFindDocDte() {
		return findDocDte;
	}
	public void setFindDocDte(String findDocDte) {
		this.findDocDte = findDocDte;
	}

	public String getFindTopType() {
		return findTopType;
	}

	public void setFindTopType(String findTopType) {
		this.findTopType = findTopType;
	}

	public String getFindType() {
		return findType;
	}

	public void setFindType(String findType) {
		this.findType = findType;
	}

	public List<KeywordTmplCnt> getListkeyTmplCnt() {
		return listkeyTmplCnt;
	}

	public void setListkeyTmplCnt(List<KeywordTmplCnt> listkeyTmplCnt) {
		this.listkeyTmplCnt = listkeyTmplCnt;
	}
}
